//
//  TranspondController.m
//  YiBaoStore
//
//  Created by 毛云河 on 2018/10/13.
//  Copyright © 2018年 小河. All rights reserved.
//

#import "TranspondController.h"

@interface TranspondController ()

@property (nonatomic, strong) UIImageView *shareImageView;
@property (nonatomic, copy) NSString *shareImageURL;

@end

@implementation TranspondController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNav];
    [self copyPasteboardString];
    [self createPhotoAndSharkBtn];
    [self createDownView];
    [self httpRequestSharkImage];
}

- (void)copyPasteboardString
{
    NSLog(@"商品标题--%@",self.shoppingDeailt);
    if (NOEmptyStr(self.shoppingDeailt))
    {
        UIPasteboard *pastea = [UIPasteboard generalPasteboard];
        pastea.string = (NSString *)[NSString stringWithFormat:@"%@",self.shoppingDeailt];
        if (pastea.string == nil)
        {
            [self ShowHUDWithMessage:@"商品描述复制失败"];
        }else
        {
            [self ShowHUDWithMessage:@"商品描述已复制，长按直接粘贴"];
        }
    }
    else {
        [self ShowHUDWithMessage:@"商品描述复制失败"];
    }
}

- (void)setNav
{
    self.titLabel.text = @"转发";
}

- (void)httpRequestSharkImage
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:self.price forKey:@"price"];
    [dic setValue:self.product_id forKey:@"itemId"];
    [dic setValue:self.template_id forKey:@"templateKey"];
    
    __weak TranspondController *myself = self;
//    [[HttpRequest currentHttpRequest] postPath:SharkImage_API params:dic resultBlock:^(id Data, MsgModel *msgModel)
//    {
//        NSLog(@"生成分享图片Data--%@",Data);
//        myself.shareImageURL = (NSString *)Data[@"result"];
//        dispatch_async(dispatch_get_main_queue(), ^{
//            [myself.shareImageView sd_setImageWithURL:[NSURL URLWithString:myself.shareImageURL] placeholderImage:[UIImage imageNamed:@"zhanwei100"]];
//        });
//    }];
    
    
    [HttpRequest formPostPath:SharkImage_API params:dic resultBlock:^(NSDictionary *Data, MsgModel *msgModel)
    {
        NSLog(@"生成分享图片Data--%@",Data);

        myself.shareImageURL = (NSString *)Data[@"result"];
        dispatch_async(dispatch_get_main_queue(), ^{
            [myself.shareImageView sd_setImageWithURL:[NSURL URLWithString:myself.shareImageURL] placeholderImage:[UIImage imageNamed:@"zhanwei100"]];
        });
    }];
}

- (void)createPhotoAndSharkBtn
{
    CGFloat imageWidth = 108.f;
    CGFloat imageHeiht = (SCREENHEIGHT/SCREENWIDTH)*imageWidth;
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, self.bgNavView.bottom, SCREENWIDTH, 1)];
    [self.view addSubview:line1];
    line1.backgroundColor = RGBACOLOR(242, 242, 242, 1);
    
    UIImageView *image;
    for (int i = 0; i < 1; i++)
    {
        image = [[UIImageView alloc] init];
        [self.view addSubview:image];
        image.contentMode = UIViewContentModeScaleAspectFit;
        if (@available(iOS 11.0, *)) {
            image.frame = CGRectMake(20+(imageWidth+10)*(i%3), (84+TopStatusHeight_xs)+(imageHeiht+10)*(i/3), imageWidth, imageHeiht);
        } else {
            // Fallback on earlier versions
            image.frame = CGRectMake(20+(imageWidth+10)*(i%3), (84+TopStatusHeight)+(imageHeiht+10)*(i/3), imageWidth, imageHeiht);
        }
        //image.image = [UIImage imageNamed:@"zhanwei100"];
        
    }
    
    self.shareImageView = image;
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(10, image.bottom+20, SCREENWIDTH-20, 1)];
    [self.view addSubview:line2];
    line2.backgroundColor = RGBACOLOR(242, 242, 242, 1);
    
    for (int i = 0; i < 2; i++)
    {
        UIImageView *leftIcon = [[UIImageView alloc] init];
        [self.view addSubview:leftIcon];
        
        UILabel *titleLabel = [[UILabel alloc] init];
        [self.view addSubview:titleLabel];
        titleLabel.font = FontWithSize(17);
        titleLabel.textColor = BlackTextColor;
        
        UIImageView *rightIcon = [[UIImageView alloc] init];
        [self.view addSubview:rightIcon];
        rightIcon.image = [UIImage imageNamed:@"next_more"];
        
        UIView *line = [[UIView alloc] init];
        [self.view addSubview:line];
        line.backgroundColor = RGBACOLOR(242, 242, 242, 1);
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.view addSubview:btn];
        btn.tag = i + 1;
        [btn addTarget:self action:@selector(selectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        leftIcon.frame = CGRectMake(15, (line2.bottom+20)+(18+40)*(i/1), 18, 18);
        titleLabel.frame = CGRectMake(leftIcon.right+15, (line2.bottom+20)+(18+40)*(i/1), 180, 18);
        rightIcon.frame = CGRectMake(SCREENWIDTH-15-16, (line2.bottom+20)+(16+40)*(i/1), 16, 16);
        line.frame = CGRectMake(15, (line2.bottom+58)+(1+58)*(i/1), SCREENWIDTH-30, 1);
        btn.frame = CGRectMake(0, line2.bottom+(58+1)*(i/1), SCREENWIDTH, 58);
        
        switch (i) {
            case 0:
                leftIcon.image = [UIImage imageNamed:@"send_watch"];
                titleLabel.text = @"发送给朋友";
                break;
                
            case 1:
                leftIcon.image = [UIImage imageNamed:@"send_quan"];
                titleLabel.text = @"发送到朋友圈";
                break;
                
            default:
                break;
        }
    }
}

#pragma mark - 创建底部view
- (void)createDownView
{
    UIView *downBgView = [[UIView alloc] init];
    if (@available(iOS 11.0, *)) {
        downBgView.frame = CGRectMake(0, SCREENHEIGHT-46-BottomHeight_xs, SCREENWIDTH, 46+BottomHeight_xs);
    } else {
        // Fallback on earlier versions
        downBgView.frame = CGRectMake(0, SCREENHEIGHT-46-BottomHeight, SCREENWIDTH, 46+BottomHeight);
    }
    [self.view addSubview:downBgView];
    downBgView.backgroundColor = [UIColor whiteColor];
    
    UIView *line = [[UIView alloc] init];
    [downBgView addSubview:line];
    line.frame = CGRectMake(0, 0, SCREENWIDTH, 1);
    line.backgroundColor = RGBACOLOR(242, 242, 242, 1);
    
    UIView *btnBgView = [[UIView alloc] init];
    [downBgView addSubview:btnBgView];
    btnBgView.frame = CGRectMake((SCREENWIDTH-106)/2, 15, 106, 16);
    
    UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"t_down"]];
    [btnBgView addSubview:icon];
    icon.frame = CGRectMake(0, 0, 16, 16);
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(26, 0, 80, 16)];
    [btnBgView addSubview:titleLabel];
    titleLabel.text = @"保存到本地";
    titleLabel.textAlignment = 1;
    titleLabel.font = FontWithSize(16);
    titleLabel.textColor = RGBACOLOR(68, 68, 68, 1);
    
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [btnBgView addSubview:saveBtn];
    saveBtn.frame = CGRectMake(0, 0, btnBgView.width, btnBgView.height);
    [saveBtn addTarget:self action:@selector(saveBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)saveBtnClick:(UIButton *)sender
{
    [self saveImageToPhotos:self.shareImageView.image];
}

#pragma mark - 保存海报到相册
- (void)saveImageToPhotos:(UIImage*)savedImage
{
    UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}

- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
    if(error != NULL)
    {
        [self ShowHUDWithMessage:@"保存失败"];
    }
    else
    {
        [self ShowHUDWithMessage:@"保存成功，快去发给好友吧"];
    }
}

- (void)selectBtnClick:(UIButton *)sender
{
    //1，调用更新状态接口
    [self updataShareStatus];
    [self.navigationController popToRootViewControllerAnimated:NO];
    //2，唤起微信分享
    switch (sender.tag) {
        case 1:
            NSLog(@"发送给朋友");
            [self sharkImageUMWith:1 withImage:self.shareImageView.image];
            break;
            
        case 2:
            NSLog(@"发送到朋友圈");
            [self sharkImageUMWith:2 withImage:self.shareImageView.image];
            break;
            
        default:
            break;
    }
}

- (void)updataShareStatus
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:self.product_id forKeyPath:@"product_id"];
    [HttpRequest formPostPath:ShareStatus_API params:dic resultBlock:^(id Data, MsgModel *msgModel)
    {
        NSLog(@"改变分享状态成功");
    }];
}


@end
